package com.css.dzj.dzjportal.modules.creditinfobase.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.css.dzj.dzjportal.common.util.http.HttpClientUtil;
import com.css.dzj.dzjportal.modules.creditinfobase.entity.Detail;
import com.css.dzj.dzjportal.modules.creditinfobase.entity.ZfjgClueEntity;
import com.css.dzj.dzjportal.modules.creditinfobase.entity.ZfjgCreditAssessEntity;
import com.css.dzj.dzjportal.modules.creditinfobase.service.CreditInfoService;
import com.querydsl.core.QueryResults;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class CreditInfoServiceImpl implements CreditInfoService {
    @Value("${ZFJG_URL}")
    private String zfjgUrl;
    /**
     * 查询信用信息的数量
     */
     public String gettotalnum(){
         try{
             String msg = HttpClientUtil.doGet(zfjgUrl + "/creditassess/gettotalnum");
             return msg;
         }catch (Exception e){
             return null;
         }

     }
    /**
     * 获取信用信息List分页显示
     */
    @Override
    public QueryResults<ZfjgCreditAssessEntity> creditInfo(Integer pageSize, Integer pageNo, String enterpriseName, String enterpriseMark1, String enterpriseMark2, String enterpriseType, PageRequest pageRequest) {
        try{
            String zfjgmsg = HttpClientUtil.doGet(zfjgUrl + "/creditassess/creditinfoList?enterpriseName="+enterpriseName+"&enterpriseMark1="+enterpriseMark1+"&enterpriseMark2="+enterpriseMark2+"&enterpriseType="+enterpriseType);
            JSONArray jsonObject = JSON.parseArray(zfjgmsg);
            List<ZfjgCreditAssessEntity> plist = new ArrayList<>();
            if(jsonObject!=null && !"".equals(jsonObject)) {
                for (int i = 0;i<jsonObject.size();i++){
                    Map<String, Object> eventMap = JSONObject.parseObject(jsonObject.get(i).toString());
                    ZfjgCreditAssessEntity zca = new ZfjgCreditAssessEntity();
                    zca.setCreditAssessId((String)eventMap.get("creditAssessId"));
                    zca.setTaskNo((String)eventMap.get("taskNo"));
                    zca.setEnterpriseName("enterpriseName");
                    zca.setEnterpriseIdType((String)eventMap.get("enterpriseIdType"));
                    zca.setEnterpriseCreditCode((String)eventMap.get("enterpriseCreditCode"));
                    zca.setEnterpriseMark((String)eventMap.get("enterpriseMark"));
                    zca.setEnterpriseType((String)eventMap.get("enterpriseType"));
                    zca.setColumnTime((String)eventMap.get("columnTime"));
                    plist.add(zca);
                }
            }
            List<ZfjgCreditAssessEntity> currentPageList = new ArrayList<>();
            if (plist != null && plist.size() > 0) {
                int currIdx = (pageNo > 1 ? (pageNo - 1) * pageSize : 0);
                for (int i = 0; i < pageSize && i < plist.size() - currIdx; i++) {
                    ZfjgCreditAssessEntity data = plist.get(currIdx + i);
                    currentPageList.add(data);
                }
            }
            QueryResults<ZfjgCreditAssessEntity> zfjgQueryResults = new QueryResults(currentPageList, (long)pageRequest.getPageSize(), pageRequest.getOffset(), plist.size());
            return zfjgQueryResults;
        }catch (Exception e){
            e.printStackTrace();
            throw new RuntimeException(e.getMessage());
        }
    }
    /**
     * 获取信用信息详情
     */
    @Override
    public Detail queryByIdNumber(String enterpriseIdNumber) {
        Detail zca = new Detail();
        try{
            String msg = HttpClientUtil.doGet(zfjgUrl + "/detail/queryById?enterpriseIdNumber="+enterpriseIdNumber);
            JSONObject jsonObject = JSON.parseObject(msg);
            zca.setId(jsonObject.getString("id"));
            zca.setEnterpriseScoreNumber(jsonObject.getString("enterpriseScoreNumber"));
            zca.setEnterpriseName( jsonObject.getString("enterpriseName"));
            zca.setEnterpriseIdNumber(jsonObject.getString("enterpriseIdNumber"));
            zca.setEnterpriseIdType(jsonObject.getString("enterpriseIdType"));
            zca.setEnterpriseScore(jsonObject.getString("enterpriseScore"));
            zca.setEnterpriseClassification(jsonObject.getString("enterpriseClassification"));
            zca.setAssessmentDate(jsonObject.getDate("assessmentDate"));
            zca.setStatus( jsonObject.getString("status"));
            zca.setFeedbackPerson(jsonObject.getString("feedbackPerson"));
            zca.setFeedbackOrg(jsonObject.getString("feedbackOrg"));
            zca.setFeedbackTime(jsonObject.getDate("feedbackTime"));
            zca.setCreateTime(jsonObject.getDate("createTime"));
            zca.setCreateUser(jsonObject.getString("createUser"));
            zca.setUpdateUser(jsonObject.getString("updateUser"));
            return zca;
        }catch (Exception e){
            e.printStackTrace();
            throw new RuntimeException(e.getMessage());
        }

    }
    /**
     * 获取信用线索企业详情
     */
    @Override
    public ZfjgClueEntity queryClueByIdNumber(String enterpriseIdNumber ) {
        ZfjgClueEntity zc = new ZfjgClueEntity();
        try{
            String msg = HttpClientUtil.doGet(zfjgUrl + "/clue/queryByIdNumber?enterpriseIdNumber="+enterpriseIdNumber);
            //Object JSONObject = JSON.parseObject(msg);
            JSONObject jsonObject=(JSONObject)JSON.parse(msg);
            zc.setId((String) ((JSONObject) jsonObject).get("id"));
            zc.setEnterpriseIdType((String) ((JSONObject) jsonObject).get("enterpriseName"));
            zc.setEnterpriseIdNumber((String) ((JSONObject) jsonObject).get("enterpriseIdNumber"));
            zc.setEnterpriseIdType((String) ((JSONObject) jsonObject).get("enterpriseIdType"));
            zc.setCreditClueReasons((String) ((JSONObject) jsonObject).get("creditClueReasons"));
            zc.setNegativeDetails((String) ((JSONObject) jsonObject).get("negativeDetails"));
            zc.setProvincial((String) ((JSONObject) jsonObject).get("provincial"));
            zc.setMunicipalLevel((String) ((JSONObject) jsonObject).get("municipalLevel"));
            zc.setCountyLevel((String) ((JSONObject) jsonObject).get("countyLevel"));
            zc.setNationalStandard1Name((String) ((JSONObject) jsonObject).get("nationalStandard1Name"));
            zc.setNationalStandard2Name((String) ((JSONObject) jsonObject).get("nationalStandard2Name"));
            zc.setNationalStandard3Name((String) ((JSONObject) jsonObject).get("nationalStandard3Name"));
            zc.setNationalStandard4Name((String) ((JSONObject) jsonObject).get("nationalStandard4Name"));
            zc.setEvaluationDate((Date) ((JSONObject) jsonObject).get("evaluationDate"));
            zc.setStatus((String) ((JSONObject) jsonObject).get("status"));
            zc.setCreateUser((String) ((JSONObject) jsonObject).get("createUser"));
            zc.setCreateTime((Date) ((JSONObject) jsonObject).get("createTime"));
            zc.setUpdateUser((String) ((JSONObject) jsonObject).get("updateUser"));
            zc.setUpdateTime((Date) ((JSONObject) jsonObject).get("updateTime"));
            return zc;
        }catch (Exception e){
            e.printStackTrace();
            throw new RuntimeException(e.getMessage());
        }
    }
}
